/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

#ifndef COMMUNICATOR_H
#define COMMUNICATOR_H

//Comment this in or out, depending if you want some debug output.
//Mainly useful to see which communication happens between Qt to Flash
#define DEBUG_OUTPUT

#include "ClassInfo.h"
#include "Entity.h"
#include "Group.h"
#include "Logger.h"

#include <QString>
#include <QStringList>
#include <QDomDocument>

class QWebFrame;

class Communicator : public QObject
{
    Q_OBJECT

public:
    Communicator( QWidget *parent, QWebFrame *frame );

signals:
    //Indicates that a entity is parsed. All coming component data belongs
    //to this entity, until parsedEntityDataEnd is emitted.
    void parsedEntityDataBegin( QString name );
    void parsedEntityDataEnd( QString name );

    //Properties: key:property name, value:property value.
    void parsedComponentData( QString name, QMap<QString, QVariant> &properties );

    void parsedLoadedSceneHierarchy( QList<IPBObject*> pbObjects );

    void parsedRequestSelectEntity( QString name );
    void parsedNewEntity( QString name, QString group );
    void parsedNewGroup( QString name, QString parent );
    void parsedDeleteObjects( QStringList objects );
    void parsedDeleteComponent( QString entityName, QString componentName );
    void parsedNewComponent( QString entityName, QString componentName, QString componentClass );

    void parsedComponentInfo( QList<ClassInfo> componentInfo );

    void parsedLevelState( QString levelState );

    void parsedGetComponentXml( QString entityName, QString componentName, QString className, QString xml );
    void parsedGetEntityXml( QString name, QString xml );
    void parsedSetEntityXml( QString name, QString xml );

public slots:
    //Parses all types of commands, and calls the coresponding parse functions.
    void parseCommands( QDomElement command );

    //Prepares the xml string and passes it on to JavaScript.
    void sendXmlString( QString xmlString );

    //Creates a new component of type componentClassName for entity with name entityName.
    void sendCreateComponent( QString entityName, QString componentName, QString componentClassName );

    //Creates a new entity with name entityName.
    void sendCreateEntity( QString entityName, QString groupName );

    void sendCreateGroup( QString groupName, QString parentGroup );

    void sendGetEntityXml( QString name );
    void sendSetEntityXml( QString name, QString xml );

    void sendGetComponentXml( QString entityName, QString componentName  );
    void sendSetComponentXml( QString entityName, QString componentName, QString xml );


    //Deletes component componentName of entity entityName.
    void sendDeleteComponent( QString entityName, QString componentName );

    void sendDeleteObjects( QStringList objects );

    //Selects the entities.
    void sendSelectEntity( QString entityName );
    void sendSelectEntities( QList<Entity*> entities );

    //Updates the property of the component to value.
    void sendUpdateComponentProperty( QString component, QString property, QString value );

    void sendLoadLevel( int index, QString name );
    void sendRequestSaveLevel( int index, QString name );
    void workspaceChanged( QString newWorkspace );

private:
    QWebFrame *frame;

//#ifdef DEBUG_OUTPUT
//    class QTextEdit*	debugText;
//#endif

    //Parses entity data, meaning this builds up the correct property browser
    //(adding all the editable fields), and selects the entity in the entity explorer.
    void parseEntityData( QDomElement node );

    //Parses scene hierarchy, meaning all the entities in the scene
    //including their components. This builds up the entity explorer.
    void parseSceneHierarchy( QDomElement entityNode );

    void parsePBObject( QDomElement objectNode, Group *currentGroup, QList<IPBObject*> *objects );

    //Parses a component list. This is needed e.g. at startup,
    //when the component browser tree is built, or the auto completion
    //for the "quick add a component" is configured.
    void parseComponentClassList( QDomElement componentNode );

    void parseRequestSelectEntity( QDomElement commandNode );
    void parseNewEntity( QDomElement entityNode );
    void parseNewGroup( QDomElement groupNode );
    void parseDeleteObjects( QDomElement objectsNode );
    void parseNewComponent( QDomElement componentNode );
    void parseDeleteComponent( QDomElement componentNode );
    void parseLevelState( QDomElement levelNode );
    void parseGetEntityXml( QDomElement commandNode );
    void parseGetComponentXml( QDomElement commandNode );

};

#endif
